package com.huijinhui.wlmtsys.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huijinhui.wlmtsys.enums.AppHttpCodeEnum;
import com.huijinhui.wlmtsys.mapper.ArticleMapper;
import com.huijinhui.wlmtsys.mapper.CommentMapper;
import com.huijinhui.wlmtsys.mapper.InfoMapper;
import com.huijinhui.wlmtsys.mapper.UserMapper;
import com.huijinhui.wlmtsys.mess.UpdateArticleMess;
import com.huijinhui.wlmtsys.pojo.dtos.CommentSaveDto;
import com.huijinhui.wlmtsys.pojo.dtos.ResponseResult;
import com.huijinhui.wlmtsys.pojo.entity.ApArticle;
import com.huijinhui.wlmtsys.pojo.entity.Comment;
import com.huijinhui.wlmtsys.pojo.entity.User;
import com.huijinhui.wlmtsys.pojo.entity.WlmtsysInfo;
import com.huijinhui.wlmtsys.service.CommentService;
import com.huijinhui.wlmtsys.service.InfoService;
import com.huijinhui.wlmtsys.utils.AppThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private MongoTemplate mongoTemplate;

    final UserMapper userMapper;

    final ArticleMapper articleMapper;
    /**
     * 评论
     *
     * @param dto
     */
    @Override
    public ResponseResult saveComment(CommentSaveDto dto) {
        // 1. 检查参数
        if (dto == null || dto.getArticleId() == null || StringUtils.isBlank(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 2. 是否登录
        User user = AppThreadLocalUtil.getUser();
        if (user == null) {
            // 如果用户未登录，返回需要登录的错误响应
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        // 3. 保存评论
        User dbUser = userMapper.selectById(user.getId());
        Comment comment = new Comment();
        comment.setAuthorId(user.getId());
        comment.setContent(dto.getContent());
        comment.setCreatedTime(new Date());
        comment.setEntryId(dto.getArticleId());
        comment.setImage(dbUser.getImage());
        comment.setAuthorName(dbUser.getUsername());
        comment.setLikes(0);
        comment.setReply(0);
        comment.setType((short) 0);
        comment.setFlag((short) 0);
        mongoTemplate.save(comment);

        UpdateArticleMess mess = new UpdateArticleMess();
        mess.setArticleId(dto.getArticleId());
        mess.setAdd(1);
        mess.setType(UpdateArticleMess.UpdateArticleType.COMMENT);
        ApArticle apArticle = articleMapper.selectById(mess.getArticleId());
        apArticle.setComment(apArticle.getComment() == null ? 0 : apArticle.getComment() + mess.getAdd());
        articleMapper.updateById(apArticle);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);


    }

}




